package com.ali.resource.ControllerPage;

import com.ali.resource.CompoentPage.DnsPrivateZoneComponent;
import com.ali.resource.CompoentPage.DnsPublicZoneComponent;
import com.ali.resource.MapPage.dns.DnsPrivateZoneMap;
import com.ali.resource.MapPage.dns.DnsPrivateZoneRecordMap;
import com.ali.resource.MapPage.dns.DnsPublicDomainMap;
import com.ali.resource.MapPage.dns.DnsPublicDomainRecordMap;
import com.ali.resource.ServicePage.DnsService;
import com.ali.resource.ShareTools.Result;
import com.aliyun.alidns20150109.models.DescribeDomainsResponseBody;
import com.aliyun.pvtz20180101.models.DescribeZonesResponseBody;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "dns接口文档" , description = "dns资源信息查询")
@RestController
@RequestMapping("/dns")
public class DnsController {

    @Autowired
    private DnsPublicZoneComponent dnsPublicZoneComponent;

    @Autowired
    private DnsService dnsService;

    @Autowired
    private DnsPrivateZoneComponent dnsPrivateZoneComponent;



    @Operation(summary = "getDomainList",description = "获取dns列表")
    @GetMapping("/getDomainList")
    public Result getDnsDomainList(@RequestParam(required = false) String domainNamePrefix) throws Exception {

        List<DnsPublicDomainMap> dnsDomains = dnsService.getDnsDomains(domainNamePrefix);
        return Result.builder().code(200).total(dnsDomains.size()).data(dnsDomains).build();
    }

    @Parameter(name = "bool", description = "是否下载至xlsx")
    @PostMapping("/getDomainRecordList")
    @Operation(summary = "getDomainRecordList",description = "获取DomainInfo")
    public Result getDnsDomainRecordLIst(
            @RequestParam(required = true,defaultValue = "false") String bool
            , @RequestParam(required = false) String hostnamePrefix
            , @RequestParam(required = false) String type
            , String domainName) throws Exception {

        String isExist = "no";

        List<DescribeDomainsResponseBody.DescribeDomainsResponseBodyDomainsDomain> domain = dnsPublicZoneComponent.getDomainResponse().getBody().getDomains().getDomain();
        for (DescribeDomainsResponseBody.DescribeDomainsResponseBodyDomainsDomain describeDomainsResponseBodyDomainsDomain : domain) {
            if (describeDomainsResponseBodyDomainsDomain.getDomainName().equals(domainName)){
                isExist = "ok";
                break;
            }
        }

        if (isExist == "ok"){

            List<DnsPublicDomainRecordMap> dnsDomainRecords = dnsService.getDnsDomainRecords(bool,hostnamePrefix,type,domainName);

            if (bool.equals("true")){
                dnsService.getExecl(dnsDomainRecords);
            }
            return Result.builder().total(dnsDomainRecords.size()).code(200).data(dnsDomainRecords).build();
        } else {
            return Result.builder().total(0).code(400).data("domainName error , please try input").build();
        }
    }

    @Operation(summary = "getPrivateRecordList",description = "获取PrivateZoneInfo")
    @Parameter(name = "bool", description = "是否下载至xlsx")
    @PostMapping("/getPrivateRecordList")
    public Result getDnsPrivateRecordLIst(
            @RequestParam(required = true,defaultValue = "false") String bool
            , @RequestParam(required = false) String hostnamePrefix
            , @RequestParam(required = false) String type
            , String zoneName) throws Exception {

        String isExist = "no";

        List<DescribeZonesResponseBody.DescribeZonesResponseBodyZonesZone> zone = dnsPrivateZoneComponent.getZoneResponse(dnsPrivateZoneComponent.getZoneRequest()).getBody().getZones().getZone();

        for (DescribeZonesResponseBody.DescribeZonesResponseBodyZonesZone describePrivatesResponseBodyDomainsDomain : zone) {
            if (describePrivatesResponseBodyDomainsDomain.getZoneName().equals(zoneName)){
                isExist = "ok";
                break;
            }
        }

        if (isExist == "ok"){

            zoneName = dnsService.getPrivateZoneList(zoneName).get(0).getZoneId();
            List<DnsPrivateZoneRecordMap> dnsPrivateZoneRecords = dnsService.getDnsPrivateZoneRecords(bool, hostnamePrefix, type, zoneName);

            if (bool.equals("true")){

                dnsService.getPrivateExecl(dnsPrivateZoneRecords);
            }
            return Result.builder().total(dnsPrivateZoneRecords.size()).code(200).data(dnsPrivateZoneRecords).build();
        } else {
            return Result.builder().total(0).code(400).data("zoneName error , please try input").build();
        }
    }

    @Operation(summary = "getPrivateZoneList",description = "获取PrivateZone列表")
    @GetMapping("/getPrivateZoneList")
    public Result getPrivateZoneList(@RequestParam(required = false) String zoneNamePrefix) throws Exception {
        List<DnsPrivateZoneMap> privateZoneList = dnsService.getPrivateZoneList(zoneNamePrefix);
        return  Result.builder().total(privateZoneList.size()).code(200).data(privateZoneList).build();
    }
}
